package com.totoro.rabbitmq.consumer;

import org.springframework.amqp.core.AcknowledgeMode;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.support.converter.ContentTypeDelegatingMessageConverter;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

@Component
public class RabbitMQListenerContainerFactory {

    @Bean
    public MessageConverter messageConverter(){
        ContentTypeDelegatingMessageConverter messageConverter = new ContentTypeDelegatingMessageConverter();
        messageConverter.addDelgate("application/json", new Jackson2JsonMessageConverter());
        return messageConverter;
    }

    @Bean
    public SimpleRabbitListenerContainerFactory simpleRabbitListenerContainerFactory(ConnectionFactory connectionFactory){
        SimpleRabbitListenerContainerFactory simpleRabbitListenerContainerFactory = new SimpleRabbitListenerContainerFactory() ;

        simpleRabbitListenerContainerFactory.setConnectionFactory(connectionFactory);
        //消息消费者需要手动确认消息已接收
        simpleRabbitListenerContainerFactory.setAcknowledgeMode(AcknowledgeMode.MANUAL);

        //设置消息序列化的方式
       // simpleRabbitListenerContainerFactory.setMessageConverter(new Jackson2JsonMessageConverter());

        return simpleRabbitListenerContainerFactory ;
    }
}
